package com.rm.zeyou.pay.wxpay.service.impl;


import com.rm.zeyou.constant.PayConstants;
import com.rm.zeyou.exception.ErrorCodeException;
import com.rm.zeyou.exception.ErrorCodeExceptionEnum;
import com.rm.zeyou.mapper.ApplicationConfigMapper;
import com.rm.zeyou.pay.wxpay.entity.WxPayEntity;
import com.rm.zeyou.pay.wxpay.service.WeChatH5ConfigService;
import com.rm.zeyou.pay.wxpay.util.WxH5Util;
import com.rm.zeyou.util.check.CheckEmptyUtil;
import com.rm.zeyou.util.redis.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @Description:
 * @Title: WeChatH5ConfigServiceImpl
 * @Package com.sxskz.software.base.login.wx.officialaccounts.service.impl
 * @Author ZhangKeChen  🤙🤙🏼🤙🏽
 * @Date 2023/4/28 3:42 PM
 */
@Service
@Slf4j
public class WeChatH5ConfigServiceImpl implements WeChatH5ConfigService {
    @Resource
    private RedisUtil redisUtil;

    @Resource
    private WxH5Util wxH5Util;

    @Resource
    private WxPayEntity wxPayEntity;
    @Resource
    private ApplicationConfigMapper applicationConfigMapper;

    @Override
    public Map<String, String> WeChatH5PayConfig(String url) {
        String accountAppid = applicationConfigMapper.selectOneData("account_appid");
        String ticket = redisUtil.get(String.format(PayConstants.WX_H5_TICKET_KEY, accountAppid));
        if (CheckEmptyUtil.isEmpty(ticket)) {
            throw new ErrorCodeException(ErrorCodeExceptionEnum.WX_H5_CONFIG_ERROR);
        }
        return wxH5Util.sign(ticket, url);
    }

}
